package com.crm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.crm.entity.Modules;
import com.crm.entity.ModulesTreeNode;
import com.crm.service.ModulesService;

@RestController
@RequestMapping("/modules")
public class ModulesController {
	
	@Autowired
	private ModulesService modulesService;
	
	/**
	* 查询所有的模块类型
	* @return List
	*/
	public List<ModulesTreeNode> getModulesTreeNodes(String rolesIds) {
		return modulesService.selectAll(rolesIds);
	}
	
	/**
	* 生成模块类型树
	* @return List
	*/
	@RequestMapping("/GetSysRightsJsonLayUI")
	public List<ModulesTreeNode> getModulesTreeNode(String rolesIds) {
		List<ModulesTreeNode> modulesTreeList = this.getModulesTreeNodes(rolesIds);
		List<ModulesTreeNode> ModulesTreeNodes = new ArrayList<ModulesTreeNode>();
		for (ModulesTreeNode modulesTreeNode : modulesTreeList) {
			if (modulesTreeNode.getParentid()==0) {
				ModulesTreeNodes.add(findChildren(modulesTreeNode, modulesTreeList));
			}
		}
		return ModulesTreeNodes;
	}
	
	/**
	* 查找子类型
	* @return List
	*/
	public ModulesTreeNode findChildren(ModulesTreeNode modulesTreeNode, List<ModulesTreeNode> modulesTreeList) {
		for (ModulesTreeNode item : modulesTreeList) {
			 if (modulesTreeNode.getId()==item.getParentid()) {
				 if(modulesTreeNode.getChildren()==null) {
					 modulesTreeNode.setChildren(new ArrayList<ModulesTreeNode>());
				 } 
				 modulesTreeNode.getChildren().add(findChildren(item,modulesTreeList));
			 } 
		}
		return modulesTreeNode;
	}
	
	/**
	 * 修改模块
	 * @param modules
	 * @return
	 */
	@RequestMapping("/UpdateModule")
	public boolean UpdateModule(Modules modules) {
		return modulesService.update(modules)>0?true:false;
	}
	
	/**
	 * 删除模块
	 * @param modules
	 * @return
	 */
	@RequestMapping("/DeleteModule")
	public boolean DeleteModule(Integer id) {
		return modulesService.deleteByPrimaryKey(id)>0?true:false;
	}
	
	/**
	 * 添加模块
	 * @param modules
	 * @return
	 */
	@RequestMapping("/CreateModule")
	public boolean CreateModule(Modules modules) {
		return modulesService.insert(modules)>0?true:false;
	}
	
	/**
	 * 查询所有模块<ModulesTreeNode>树形
	 * @return
	 */
	@RequestMapping("/selectAllModule")
	public List<ModulesTreeNode> selectAllModule() {
		return modulesService.selectAllModulesTree();
	}
	
	/**
	 * 查询所有模块<Modules>
	 * @return
	 */
	@RequestMapping("/GetModuleAll")
	public List<Modules> GetModuleAll(){
		return modulesService.selectListAll();
	}
	
	/**
	 * 根据角色id查询全部模块
	 * @param id
	 * @return
	 */
	@RequestMapping("/getModuleByUid")
	public Map<String, Object> getModuleByUid(String id) {
		Map<String, Object> map = new HashMap<>();
		List<Modules> modList = modulesService.selectRoleIdListAll(id);
		map.put("code", "0");
		map.put("msg", "success");
		map.put("count", modList.size());
		map.put("data", modList);
		return map;
	}
	
}
